package com.aixin.project.servlet.admin;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;

public abstract class AdminServlet extends VelocityViewServlet {
	Logger logger = Logger.getLogger(AdminServlet.class);

	@Override
	protected Template handleRequest(HttpServletRequest request,
			HttpServletResponse response, Context context) {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        long start = System.currentTimeMillis();
		Template template = execute(request, response, context);
		long end = System.currentTimeMillis();
		System.out.println(request.getPathInfo() + " page execute time :"
				+ (end - start));
		return template;
	}

	protected abstract Template execute(HttpServletRequest request,
			HttpServletResponse response, Context ctx);

	protected void mergeTemplate(Template template, Context context,
			HttpServletResponse response) throws IOException {
		if (null != template)
			super.mergeTemplate(template, context, response);
	}

	protected void sendRedirect(HttpServletResponse response, String url) {
		try {
			response.sendRedirect(url);
		} catch (IOException e) {
			logger.error("redirect url error: " + url);
		}
	}
}
